$(function(){
	var importM = ["depCode", "depName", "wardCode", "wardName", "userName", "userCode", "password", "role", "telephone", "email", "description"];
	$("#"+tabId+" #save").click(function(){
		var str = $("textarea").val();
	    var len = str.split("\n");//获取行数
	    var ajaxCount = 0;
	    var errorMessage = "";
		for(var i=0;i<len.length;i++){
			var trStr = len[i].split(/\s/); 
			if(trStr.length<11) continue;
			var param = {};
			for(var j=0;j<=trStr.length-1;j++){
				param[importM[j]] = trStr[j];
			}
			$.post(contextPath+"/auth/user/userImport.dhc", {param: JSON.stringify(param)}).done(function(data){
				if(data.hasErrors){
					errorMessage += data.errorMessage+"\n";
				}
				ajaxCount--;
			})
			ajaxCount++;
	    }
		var timer = setInterval(function(){
			if(ajaxCount==0){
				clearInterval(timer);
				if(errorMessage!=""){
					 drgUI.error(errorMessage);
				}else{
					drgUI.success('导入完毕!');
				}
			}
		}, 100);
	})
	
    $("textarea").on('keydown',function(e){
        if(e.keyCode == 9){
            e.preventDefault();
            var indent = '\t';
            var start = this.selectionStart;
            var end = this.selectionEnd;
            var selected = window.getSelection().toString();
            selected = indent + selected.replace(/\n/g,'\n'+indent);
            this.value = this.value.substring(0,start) + selected + this.value.substring(end);
            this.setSelectionRange(start+indent.length,start+selected.length);
        }
        if(e.keyCode == 13){
            e.preventDefault();
            var indent = '\n';
            var start = this.selectionStart;
            var end = this.selectionEnd;
            var selected = window.getSelection().toString();
            selected = selected.replace(/\n/g,'\n'+indent)+indent;
            this.value = this.value.substring(0,start) + selected + this.value.substring(end);
            this.setSelectionRange(start+indent.length,start+selected.length);
        }
    })
})
